package com.mvu.banana.guest.server;

import com.mvu.banana.domain.stub.Credential;
import com.mvu.banana.domain.stub.Profile;
import com.mvu.banana.guest.client.gen.SignUpFormDTO;
import com.mvu.banana.guest.server.gen.SignUpFormServletStub;
import org.jasypt.util.password.BasicPasswordEncryptor;

/**
 * Class description...
 *
 * @author gantz
 */
public class SignUpFormServlet extends SignUpFormServletStub {
  public SignUpFormDTO post(SignUpFormDTO dto) {
    System.out.println("Signing up: " + dto.toString());
    Profile profile = createProfile(dto);
    profile.persist();
    Credential credential = new Credential();
    credential.setUsername(dto.email);
    BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
    credential.setPassword(passwordEncryptor.encryptPassword(dto.password));
    // TODO permissions
    credential.getProfiles().add(profile);
    credential.persist();
    copy(profile, dto);
    return dto;
  }
}
